<?php


namespace frontend\modules\wechat\controllers;


use common\models\wechat\WechatBasicReply;
use common\models\wechat\WechatImagesReply;
use common\models\wechat\WechatReplyKeyword;
use common\models\wechat\WechatVoiceReply;

class ReplyController extends ProcessController
{

    //回复文字
    public function actionText($content = ''){
        if(empty($content)){
            $content = $this->message['Content'];
        }
        $kwd = WechatReplyKeyword::find()->asArray()->select('a.rid,a.content')->leftJoin(WechatBasicReply::tablename().' as a',WechatReplyKeyword::tablename().'.rid=a.rid')->where([WechatReplyKeyword::tablename().'.content'=>$content])->one();
        return $this->responseText($kwd['content']);
    }


    //回复图片
    public function actionImage($content = ''){
        if(empty($content)){
            $content = $this->message['Content'];
        }
        $kwd = WechatImagesReply::find()->asArray()->select('a.rid,mediaid')->innerJoin(WechatReplyKeyword::tablename().' as a',WechatImagesReply::tablename().'.rid=a.rid')->where(['content'=>$content])->one();
        return $this->responseImage($kwd['mediaid']);


    }


    //回复图文
    public function actionNews($content = ''){
        if(empty($content)){
            $content = $this->message['Content'];
        }

        $list = WechatNewsReply::find()->asArray()->select('title,description,thumb,url')->innerJoin(WechatReplyKeyword::tablename().' as a',WechatNewsReply::tablename().'.rid=a.rid')->where(['content'=>$content])->all();

        foreach ($list as $key => $value) {
            $articles['title'] = $value['title'];
            $articles['description'] = $value['description'];
            $articles['thumb'] = Yii::getAlias('@uploadsite').'/'.$value['thumb'];
            $articles['url'] = $value['url'];
        }
        return $this->responseNews($articles);
    }



    //回复语音
    public function actionVoice($content = ''){
        if(empty($content)){
            $content = $this->message['Content'];
        }

        $kwd = WechatVoiceReply::find()->asArray()->select('a.rid,mediaid')->innerJoin(WechatReplyKeyword::tablename().' as a',WechatVoiceReply::tablename().'.rid=a.rid')->where(['content'=>$content])->one();
        return $this->responseVoice($kwd['mediaid']);
    }




}
